package org.csu.onlineedubackend.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import org.csu.onlineedubackend.entity.Notification;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.result.ResponseCode;
import org.csu.onlineedubackend.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    private Integer getUserIdFromRequest(HttpServletRequest request) throws NotLoginException {
        try {
            return StpUtil.getLoginIdAsInt();
        } catch (NotLoginException e) {
            String token = request.getHeader("satoken");
            if (token == null || token.isEmpty()) {
                token = request.getParameter("token");
            }

            if (token != null && !token.isEmpty()) {
                Object loginId = StpUtil.getLoginIdByToken(token);
                if (loginId != null) {
                    return Integer.parseInt(loginId.toString());
                }
            }
            throw new NotLoginException("未登录或登录已过期，请重新登录", e.getType(), e.getLoginType());
        }
    }

    @PostMapping
    public CommonResponse<Notification> createNotification(@RequestBody Notification notification, @RequestParam Integer userId) {
        try {
            // 确保通知的用户ID与当前登录用户一致
            if (!notification.getUserId().equals(userId)) {
                return CommonResponse.createForError("无权为其他用户创建通知");
            }
            // 添加通知
            notificationService.addNotification(notification);
            return CommonResponse.createForSuccess(10000, "创建通知成功", notification);
        } catch (NotLoginException e) {
            return CommonResponse.createForError("未登录或登录已过期，请重新登录");
        } catch (Exception e) {
            return CommonResponse.createForError("创建通知失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public CommonResponse<List<Notification>> getUserNotifications(
            @RequestParam Integer userId
            ) {
        try {
            List<Notification> notifications = notificationService.getUserNotifications(userId);
            return CommonResponse.createForSuccess(
                10000,
                "获取通知列表成功",
                notifications
            );
        } catch (NotLoginException e) {
            return CommonResponse.createForError("未登录或登录已过期，请重新登录");
        }
    }

    @PostMapping("/read/all")
    public CommonResponse<Void> markAllAsRead(@RequestParam Integer userId) {
        try {
            notificationService.markAllAsRead(userId);
            return CommonResponse.createForSuccess(
                10000,
                "标记所有通知为已读成功",
                null
            );
        } catch (NotLoginException e) {
            return CommonResponse.createForError("未登录或登录已过期，请重新登录");
        }
    }

    @PostMapping("/read/{notificationId}")
    public CommonResponse<Void> markAsRead(@PathVariable Long notificationId, @RequestParam Integer userId) {
        try {
            notificationService.markAsRead(notificationId, userId);
            return CommonResponse.createForSuccess(
                10000,
                "标记通知为已读成功",
                null
            );
        } catch (NotLoginException e) {
            return CommonResponse.createForError("未登录或登录已过期，请重新登录");
        }
    }
} 